//cullingMode
//POLYGON_OFFSET_FILL, POLYGON_OFFSET_FACTOR POLYGON_OFFSET_UNITS
namespace FIREFLYX {
    /** 渲染面剔除模式 */
    export enum CullMode {
        /** 不剔除 */
        NONE = 0,
        /** 剔除背面 */
        BACK = 1,
        /** 剔除前面 */
        FRONT = 2,
    }

    /** 光栅状态 */
    export class RasterState {
        private static cacheStateMap: { [key: string]: RasterState } = {};
        private states: number[];
        // eslint-disable-next-line no-useless-constructor
        private constructor() {
            //
        }

        /** 面剔除模式 */
        public get cullMode(): CullMode { return this.states[0]; }
        /** 多边形偏移因子值 (默认0) */
        public get polygonOffsetFactor(): number { return this.states[1]; }
        /** 多边形偏移单元值 (默认0)*/
        public get polygonOffsetUnits(): number { return this.states[2]; }

        /**
         * 获取光栅状态
         * @param cullMode 面剔除模式 
         * @param polygonOffsetFactor 多边形偏移因子值
         * @param polygonOffsetUnits 多边形偏移单元值
         * @returns 光栅状态对象
         */
        public static getState(cullMode: CullMode = CullMode.BACK, polygonOffsetFactor: number = 0, polygonOffsetUnits: number = 0) {
            let stateKey = `${cullMode}_${polygonOffsetFactor}_${polygonOffsetUnits}`;
            if (isNaN(polygonOffsetFactor)) polygonOffsetFactor = 0;
            if (isNaN(polygonOffsetUnits)) polygonOffsetUnits = 0;
            let enablePlgOffset = polygonOffsetFactor == 0 && polygonOffsetUnits == 0;
            if (enablePlgOffset) {
                //限定小数位
                polygonOffsetFactor = Number(polygonOffsetFactor.toFixed(1));
                polygonOffsetUnits = Number(polygonOffsetUnits.toFixed(1));
            }
            let state = this.cacheStateMap[stateKey];
            if (!state) {
                this.cacheStateMap[stateKey] = state = new RasterState();
                state.states = [cullMode, polygonOffsetFactor, polygonOffsetUnits];
            }
            return state;
        }
    }
}